package com.mfangsoft.zhuangjialong.integration.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.solr.common.util.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.integration.product.mapper.BrandProductEntityMapper;
import com.mfangsoft.zhuangjialong.integration.product.mapper.BrandProductImgEntityMapper;
import com.mfangsoft.zhuangjialong.integration.product.mapper.BrandProductSalesattrEntityMapper;
import com.mfangsoft.zhuangjialong.integration.product.mapper.BrandProductSeriesEntityMapper;
import com.mfangsoft.zhuangjialong.integration.product.model.BrandProductEntity;
import com.mfangsoft.zhuangjialong.integration.product.model.BrandProductImgEntity;
import com.mfangsoft.zhuangjialong.integration.product.model.BrandProductSalesattrEntity;
import com.mfangsoft.zhuangjialong.integration.product.model.BrandProductSeriesEntity;
import com.mfangsoft.zhuangjialong.integration.product.service.ProductService;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;
@Service
public class ProductServiceImpl implements  ProductService{
	
	@Autowired
	private  BrandProductEntityMapper brandProductEntityMapper;
	
	@Autowired
	private  BrandProductImgEntityMapper brandProductImgEntityMapper;
	
	@Autowired
	private  BrandProductSeriesEntityMapper  brandProductSeriesEntityMapper;

	@Autowired
	private  BrandProductSalesattrEntityMapper  brandProductSalesattrEntityMapper;
	
	@Autowired
	private  UserService   userService;
	
	@Override
	public Boolean addProduct(BrandProductEntity brandProductEntity) {
		// TODO Auto-generated method stub
		
		brandProductEntity.setCreate_time(new Date());
		
		brandProductEntity.setState(0);
		
		brandProductEntity.setCreater(UserContext.getCurrentUserId());
		
		brandProductEntity.setUser_id(UserContext.getCurrentUserId());
		
		brandProductEntityMapper.insertSelective(brandProductEntity);
		
		List<BrandProductImgEntity> brandProductImgEntities=brandProductEntity.getBrandProductImgEntities();
		
		for (int i=0;i< brandProductImgEntities.size();i++) {
			
			BrandProductImgEntity brandProductImgEntity=brandProductImgEntities.get(i);
			
			brandProductImgEntity.setProduct_id(brandProductEntity.getId());
			
			brandProductImgEntity.setUser_id(UserContext.getCurrentUserId());
			
			brandProductImgEntity.setImg_sort(i);
			
			brandProductImgEntityMapper.insertSelective(brandProductImgEntity);
		}
		
		List<BrandProductSeriesEntity> brandProductSeriesEntities=brandProductEntity.getBrandProductSeriesEntities();
		
		
		for (BrandProductSeriesEntity brandProductSeriesEntity : brandProductSeriesEntities) {
			
			brandProductSeriesEntity.setProduct_id(brandProductEntity.getId());
			
			
			brandProductSeriesEntity.setUser_id(UserContext.getCurrentUserId());
			
			
			brandProductSeriesEntityMapper.insertSelective(brandProductSeriesEntity);
			
		}
		
		List<BrandProductSalesattrEntity> brandProductSalesattrEntities=brandProductEntity.getBrandProductSalesattrEntities();
		
		for (BrandProductSalesattrEntity brandProductSalesattrEntity : brandProductSalesattrEntities) {
			
			brandProductSalesattrEntity.setProduct_id(brandProductEntity.getId());
			
			brandProductSalesattrEntity.setUser_id(UserContext.getCurrentUserId());
			
			brandProductSalesattrEntityMapper.insertSelective(brandProductSalesattrEntity);
			
		}
		
		return true;
	}

	@Override
	public Boolean modifyProduct(BrandProductEntity brandProductEntity) {
		// TODO Auto-generated method stub
		
		
		brandProductEntityMapper.updateByPrimaryKeySelective(brandProductEntity);
		
		List<BrandProductImgEntity> brandProductImgEntities=brandProductEntity.getBrandProductImgEntities();
		
		for (int i=0;i< brandProductImgEntities.size();i++) {
			
			BrandProductImgEntity brandProductImgEntity=brandProductImgEntities.get(i);
			
			brandProductImgEntity.setProduct_id(brandProductEntity.getId());
			
			brandProductImgEntity.setUser_id(UserContext.getCurrentUserId());
			
			brandProductImgEntity.setImg_sort(i);
			
			brandProductImgEntityMapper.updateByPrimaryKeySelective(brandProductImgEntity);
		}
		
		
		
		
		
		List<BrandProductSeriesEntity> brandProductSeriesEntities=brandProductEntity.getBrandProductSeriesEntities();
		
		brandProductSeriesEntityMapper.deleteByProduct(brandProductEntity.getId());
		for (BrandProductSeriesEntity brandProductSeriesEntity : brandProductSeriesEntities) {
			
			brandProductSeriesEntity.setProduct_id(brandProductEntity.getId());
			
			
			brandProductSeriesEntity.setUser_id(UserContext.getCurrentUserId());
			
			
			brandProductSeriesEntityMapper.insertSelective(brandProductSeriesEntity);
			
		}
		
		List<BrandProductSalesattrEntity> brandProductSalesattrEntities=brandProductEntity.getBrandProductSalesattrEntities();
		
		for (BrandProductSalesattrEntity brandProductSalesattrEntity : brandProductSalesattrEntities) {
			
			brandProductSalesattrEntity.setProduct_id(brandProductEntity.getId());
			
			brandProductSalesattrEntity.setUser_id(UserContext.getCurrentUserId());
			
			if(brandProductSalesattrEntity.getId()==null){
				
				brandProductSalesattrEntityMapper.insert(brandProductSalesattrEntity);
			}else{
				
				brandProductSalesattrEntityMapper.updateByPrimaryKeySelective(brandProductSalesattrEntity);
			}
			
			
			
		}
		
		
		return true;
	}

	@Override
	public BrandProductEntity getProductById(Long id) {
		// TODO Auto-generated method stub
		BrandProductEntity brandProductEntity=brandProductEntityMapper.selectByPrimaryKey(id);
		
		
		brandProductEntity.setBrandProductImgEntities(brandProductImgEntityMapper.selectByProductId(id));
		
		brandProductEntity.setBrandProductSalesattrEntities(brandProductSalesattrEntityMapper.selectByProductId(id));
		
		brandProductEntity.setBrandProductSeriesEntities(brandProductSeriesEntityMapper.selectByProductId(id));
		
		return brandProductEntity;
	}

	@Override
	public Page<Map<String, Object>> queryProductForPage(Page<Map<String, Object>> page) {
		// TODO Auto-generated method stub
		
		
		Map<String, Object> map=(Map<String, Object>) page.getParam();
		
		if(map.get("iswarehouse")!=null){
			
			map.put("user_id", UserContext.getCurrentUser().getUserRelationEntity().getNode3());
			
		}else if(UserContext.getCurrentUser().getUser_level().intValue()!=1){
			
			List<Map<String,Object>>  m=userService.getUserByLevel(UserContext.getCurrentUser().getUser_level(), UserContext.getCurrentUserId(), null,1);
			
			StringBuffer  bu= new StringBuffer();
			
			for (int i = 0; i < m.size(); i++) {
				
				Map<String,Object> ids= m.get(i);
				
				
				if(i==m.size()-1){
					bu.append(ids.get("id").toString());
				}else{
					bu.append(ids.get("id").toString());
					bu.append(",");
				}
				
			}
			
			map.put("userids", bu.toString());
			
		}else{
			
			map.put("user_id", UserContext.getCurrentUserId());
		}
		List<Map<String, Object>>  list=brandProductEntityMapper.queryProductForPage(page);
		
		page.setData(list);
		return page;
	}

	@Override
	public Boolean removeProductSaleattr(Long id) {
		// TODO Auto-generated method stub
		
		brandProductSalesattrEntityMapper.deleteByPrimaryKey(id);
		return true;
	}

	@Override
	public Boolean setProductPrice(Integer  type, Boolean value) {
		// TODO Auto-generated method stub
		
		
		Map<String,Object>  map= new HashMap<>();
		
		List<Long> userIds= new ArrayList<>();
		
		if(type.intValue()==1){
			
			if(value){
				
				map.put("update_sql", "set isvisible=1");
			}else{
				map.put("update_sql", "set isvisible=0");
			}
			
			List<Map<String,Object>> list=userService.getUserByLevel(UserContext.getCurrentUser().getUser_level(), UserContext.getCurrentUserId(), null,1);
			
			
			
			for (Map<String, Object> map2 : list) {
				
				userIds.add(new Long(map2.get("id").toString()));
			}
			userIds.add(UserContext.getCurrentUserId());
			
			map.put("userId", userIds);
			
		}else{
			
			if(value){
				
				map.put("update_sql", "set isedit=1");
			}else{
				map.put("update_sql", "set isedit=0");
			}
			userIds.add(UserContext.getCurrentUserId());
			
			map.put("userId", userIds);
		}
		brandProductSalesattrEntityMapper.updateSaleasattr(map);
		return true;
	}

	@Override
	public Boolean copyProduct(String ids) {
		// TODO Auto-generated method stub
		
		
		Map<String,Object>  map = new HashMap<>();
		if(ids.equals("-1")){
			
			map.put("where_sql", " img.user_id ="+UserContext.getCurrentUser().getUserRelationEntity().getNode3());
		}else{
			
			map.put("where_sql", "id in ("+ids+")");
		}
		map.put("user_id", UserContext.getCurrentUserId());
		
		
		brandProductEntityMapper.copyProduct(map);
		brandProductEntityMapper.copyProductImg(map);
		brandProductEntityMapper.copyProductSales(map);
		
		return null;
	}

	@Override
	public Boolean updateBrandProduct(List<Long> list, Integer state) {
		// TODO Auto-generated method stub
		
		for (Long long1 : list) {
			Map<String,Object> map = new HashMap<>();
			
			map.put("state", state);
			map.put("id", long1);
			brandProductEntityMapper.updateBrandProduct(map);
		}
		
		return true;
	}

}
